//
//  YWFunctionTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/5.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWFunctionTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImageView+WebCache.h"
#import "YWFunctionModel.h"
#import "YYLabel.h"

@interface YWFunctionTableViewCell()
@property (nonatomic, strong) UIImageView * functionImageV;
@property (nonatomic, strong) YYLabel * functionNameL;
@property (nonatomic, strong) UISwitch * turnSwitch;
@end

@implementation YWFunctionTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setFunction:(YWFunctionModel *)function{
    _function = function;
    _functionImageV.image = [UIImage imageNamed:function.imageString];
    _functionNameL.text = function.functionName;
    if (function.offOrOn == YWFunctionStatusOn) {
        [_turnSwitch setOn:YES];
    }else{
        [_turnSwitch setOn:NO];
    }
}

- (void)setUpSubviews{
    [self.functionImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(8);
        make.left.mas_equalTo(self.contentView).offset(15);
        make.width.and.height.mas_equalTo(@32);
    }];
    
    [self.functionNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.functionImageV.mas_right).offset(12);
        make.top.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.turnSwitch.mas_left).offset(-12);
        make.bottom.mas_equalTo(self.contentView).offset(-8);
    }];
    [self.turnSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-15);
        make.top.mas_equalTo(self.contentView).offset(10);
        make.bottom.mas_equalTo(self.contentView).offset(10);
    }];
}

- (UIImageView *)functionImageV{
    if (!_functionImageV) {
        UIImageView *imageView = [UIImageView new];
        imageView.layer.masksToBounds = YES;
        _functionImageV = imageView;
        [self.contentView addSubview:_functionImageV];
    }
    return _functionImageV;
}

- (YYLabel *)functionNameL{
    if (!_functionNameL) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
//        label.textColor = [UIColor customTextGrayBlackColor];
        _functionNameL = label;
        [self.contentView addSubview:_functionNameL];
    }
    return _functionNameL;
}

- (UISwitch*)turnSwitch{
    if (!_turnSwitch) {
        UISwitch * turnSwitch = [[UISwitch alloc] init];
        [turnSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
//        _turnSwitch.onTintColor = [UIColor greenColor];
        _turnSwitch = turnSwitch;
        [self.contentView addSubview:_turnSwitch];
    }
    return _turnSwitch;
}

-(void)switchAction:(UISwitch*)switchButton{
    if ([self.delegate respondsToSelector:@selector(clickSwitchEvent:switchButton:offOrOn:)]) {
        [self.delegate clickSwitchEvent:self switchButton:switchButton offOrOn:switchButton.isOn];
    }
}

@end
